/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iagente;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.Timer;

/**
 *
 * @author Note
 */
public class MatrizQVisual extends JFrame implements ActionListener {
    
    private Timer animationTimer;
    private final int widthX = 600;
    private final int heightY = 600;
    private int animationDelay = 100;
    private int animCount = 0;
    private BufferStrategy strategy;
    
    
    public void initialize()
	{
        setSize(widthX, heightY);
        //setUndecorated (true); //Removes screen accessory(title bar, side bars)
        setBackground(Color.BLACK);
        setTitle("Matriz Q");
        
        setResizable(false);
        setVisible(true);
        
        //Buffer strategy
        createBufferStrategy(2);
		strategy = getBufferStrategy();
		
		startAnimation();

		//KEY listeners
        addKeyListener(new KeyAdapter() 
		{
            public void keyPressed(KeyEvent kevent)
			{
				// ESC key listener
                if (kevent.getKeyCode() == 27)
				{
					setVisible(false);
                }
            }
        });
    }
	
	public void terminate()
	{
		setVisible(false);
        dispose();
	}
	

   
    public void paint(Graphics g1)
    {   
		
        Graphics2D g2 = (Graphics2D) strategy.getDrawGraphics();
        g2.clearRect(0, 0, widthX, heightY);
		
		double[] matrix = MatrizQ.getInstance().getQ_matrix();
	
		for(int y = 0; y < Interfaz.y_colu; y++)
			for(int x = 0; x < Interfaz.x_fila; x++)
			{
				double color = matrix[y*Interfaz.x_fila + x];
				double matrixVal = color;
				
				if(color < 0)
					g2.setColor(new Color(230, 0, 0));
			
				else if(color > 0)
				{
					double max = MatrizQ.getInstance().getMaxQMatrixValue();
					
					if(max <= 0) max = 1;
					
					color = color * (255 / max );
					color += 30;
					
					if(color > 255) color = 255;
					
					g2.setColor(new Color(0, (int)color, 0));
				}
				
				else if(color == 0)
					g2.setColor(new Color(150, 150, 150));
				
				
				g2.fillRect(x*50, 25 + y*50, 50, 50);
				
				g2.setColor(Color.WHITE);
				g2.drawString((int)matrixVal+"", x*50 + 10, 50 + y*50);
			}
		
		strategy.show();      
		animCount++;
    }

     
    public void startAnimation()
	{
        if (animationTimer == null)
		{
            animationTimer = new Timer(animationDelay, this);
            animationTimer.start();
        }
    }

    public void stopAnimation()
	{
        animationTimer.stop();
    }

    public void actionPerformed(ActionEvent arg0)
	{
        repaint();
    }
}
